package com.atjava.webspider.gui.data;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;

import org.apache.commons.lang.StringUtils;

import com.atjava.jox.io.JOXWriter;
import com.atjava.webspider.consts.Const;
import com.atjava.webspider.gui.SpiderMainGui;
import com.atjava.webspider.gui.output.NewOutputGui;
import com.atjava.webspider.gui.output.NewVarGui;
import com.atjava.webspider.gui.table.DefaultTable;
import com.atjava.webspider.gui.table.DefaultTableModel;
import com.atjava.webspider.gui.task.NewTaskGui;
import com.atjava.webspider.gui.tree.JFileTree;
import com.atjava.webspider.gui.tree.TaskFileFilter;
import com.atjava.webspider.gui.tree.JFileTree.FileNode;
import com.atjava.webspider.logic.ConfigLogic;
import com.atjava.webspider.logic.ResourceLogic;
import com.atjava.webspider.model.Output;
import com.atjava.webspider.model.Replace;
import com.atjava.webspider.model.Resource;
import com.atjava.webspider.model.Task;
import com.atjava.webspider.services.TableService;
import com.atjava.webspider.services.ToolbarService;
import com.atjava.webspider.util.DateUtil;
import com.atjava.webspider.util.DialogUtil;
import com.atjava.webspider.util.TableUtil;
import javax.swing.border.TitledBorder;

public class DataGui extends JDialog {

	private static final long serialVersionUID = 1L;
	
	private JPanel jContentPane = null;
	private JSplitPane jSplitPaneMain = null;
	private JScrollPane jPanelLeft = null;
	private JFileTree jTree = null;
	private JScrollPane jScrollPaneTaskList = null;
	private JTable jTableDataList = null;
	private DefaultTableModel dataModel = null;
	
	/**
	 * @param owner
	 */
	public DataGui(Frame owner,boolean modal) {
		this(owner,modal,new Task());
	}
	
	public DataGui(Frame owner,boolean modal,Task task) {
		super(owner,modal);
		initialize();
		//setTextValue();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(800, 556);
		this.setResizable(false);
		this.setTitle("数据管理");
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(getOwner());
		this.setModal(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJSplitPaneMain(), null);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jSplitPaneMain	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getJSplitPaneMain() {
		if (jSplitPaneMain == null) {
			jSplitPaneMain = new JSplitPane();
			jSplitPaneMain.setDividerSize(3);
			jSplitPaneMain.setBounds(new Rectangle(1, 0, 791, 493));
			jSplitPaneMain.setLeftComponent(getJPanelLeft());
			jSplitPaneMain.setRightComponent(getJScrollPaneTaskList());
			jSplitPaneMain.setDividerLocation(160);
		}
		return jSplitPaneMain;
	}
	
	/**
	 * This method initializes jTree	
	 * 	
	 * @return javax.swing.JTree	
	 */
	public JFileTree getJTree() {
		if (jTree == null) {
			try {
				File taskHome = new File(Const.TASK_HOME);
				FileNode root = new FileNode(taskHome,new TaskFileFilter());
				jTree = new JFileTree(root,this);
				jTree.setSelectFile(taskHome);
				jTree.expandAll();
			} catch (Exception e) {
				e.printStackTrace();
			}
			jTree.setForeground(new Color(51, 51, 51));
			jTree.setSize(new Dimension(100, 300));
			jTree.setBounds(new Rectangle(1, 2, 100, 300));
			jTree.setLocation(new Point(1, 2));
			jTree.setFont(new Font("Monospaced", Font.PLAIN, 12));
		}
		return jTree;
	}

	/**
	 * This method initializes jPanelLeft	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JScrollPane getJPanelLeft() {
		if (jPanelLeft == null) {
			jPanelLeft = new JScrollPane();
			//jPanelLeft.setLayout(null);
			//jPanelLeft.add(getJTree(), null);
			jPanelLeft.setBorder(BorderFactory.createLineBorder(Color.gray, 0));
			jPanelLeft.setViewportView(getJTree());
		}
		return jPanelLeft;
	}
	
	/**
	 * This method initializes jScrollPaneTaskList	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneTaskList() {
		if (jScrollPaneTaskList == null) {
			jScrollPaneTaskList = new JScrollPane();
			jScrollPaneTaskList.setViewportView(getJTableTaskList());
			jScrollPaneTaskList.setBorder(BorderFactory.createLineBorder(Color.gray, 0));

		}
		return jScrollPaneTaskList;
	}

	/**
	 * This method initializes jTableTaskList	
	 * 	
	 * @return javax.swing.JTable	
	 */
	public JTable getJTableTaskList() {
		if (jTableDataList == null) {
			String [] colums = {"文件名","保存路径","大　小","采集时间"};
			try {
				TableService tabService = TableService.getInstance();
				dataModel = new DefaultTableModel(colums);
				jTableDataList = new DefaultTable(dataModel);
				TableUtil.setTableAlign(jTableDataList, JLabel.CENTER);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//自动调整大小
			TableUtil.fitTableColumns(jTableDataList,new int[]{200,50,50,50});
			//默认渲染方式
			DefaultTableCellRenderer r = new DefaultTableCellRenderer();   
			r.setHorizontalAlignment(JLabel.CENTER);
			jTableDataList.setDefaultRenderer(Object.class,r);
			
			jTableDataList.setSize(new Dimension(620, 80));
			jTableDataList.setRowHeight(25);
			jTableDataList.setRequestFocusEnabled(false);
			jTableDataList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jTableDataList.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
				public void valueChanged(ListSelectionEvent evt) {
					
				}
			});
			jTableDataList.addMouseListener(new MouseAdapter() {

				public void mouseClicked(MouseEvent e) {
					/*if(!SpiderMainGui.this.jButtonProp.isEnabled())
						return;
					int n = getSelectRow();
					if(n >= 0){
						Task t = getCurrentTaskList().get(n);
						NewTaskGui tgui = new NewTaskGui(SpiderMainGui.this,true,t);
						tgui.setVisible(true);
					}*/
				}
				
			});
		}
		return jTableDataList;
	}

	public DefaultTableModel getDataModel() {
		return dataModel;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
